package com.cn.http.service;

import cn.com.allunion.sms.pojo.Smsrecord;
import cn.com.allunion.sms.service.mybatis.SmsrecordService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cn.jms.SendSMS;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 抽象短信发送服务实现类
 * Created by YangJie on 2016/4/21.
 */
public abstract class AbstractSmsSendService implements SmsSendService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSmsSendService.class) ;
    /**
     * 短信记录业务逻辑类
     */
    protected SmsrecordService smsrecordService ;

    /**
     * 消息发送服务
     */
    protected SendSMS sendSMS ;


    /**
     * 合并电话号码，电话号码之间使用,间隔，例如：139xxxxx,138xxxxx,182xxxxx
     * @param smsRecords 短信描述对象集合
     * @return 拼接好的电话号码字符串，传入参数为空或者空集合，返回空字符串 ""
     */
    protected String concatPhoneNumber(List<Smsrecord> smsRecords) {
        if (CollectionUtils.isEmpty(smsRecords)) { // 集合为空
            return "" ;
        }

        StringBuilder strBuilder = new StringBuilder() ;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("---> 开始拼接电话号码 >");
        }
        for (Smsrecord sRecord : smsRecords) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(sRecord.getSendNumber());
            }
            strBuilder.append(sRecord.getSendNumber()).append(",") ;
        }
        strBuilder.deleteCharAt(strBuilder.length()-1) ;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("---> 电话号码拼接结果 =" + strBuilder.toString());
        }
        return strBuilder.toString() ;
    }

    /**
     * 获取发送到消息队列的字符串
     * @param smsrecord 短信记录
     * @param sign 签名
     * @return 要发送的消息内容，如果传递参数有一个为空，那么返回null
     */
    protected String getSendMessage(Smsrecord smsrecord, String sign) {
        if (null == smsrecord || StringUtils.isEmpty(sign)) {
            LOGGER.warn("获取发送消息字符串警告， smsrecord: {}, sign: {}", smsrecord, sign);
            return null ;
        }

        JSONObject jsObject = new JSONObject(true) ;
        jsObject.put("operating", null) ;
        jsObject.put("smsOperator", smsrecord.getSmsOperator().getJoinNum()) ;
        jsObject.put("operatorMessageID", smsrecord.getOperatorMessageID()) ;
        jsObject.put("sendContent", smsrecord.getSendContent()) ;
        jsObject.put("sendNumber", smsrecord.getSendNumber()) ;
        jsObject.put("sendState", smsrecord.getSendState()) ;
        jsObject.put("smsRecordID", smsrecord.getSmsRecordID()) ;
        jsObject.put("businessRecord", smsrecord.getSmsBusinessRecord().getBusinessCode()) ;
        jsObject.put("sign", "【" + sign + "】") ;

        return JSON.toJSONString(jsObject, SerializerFeature.WriteMapNullValue) ;
    }


    public void setSmsrecordService(SmsrecordService smsrecordService) {
        this.smsrecordService = smsrecordService;
    }

    public SendSMS getSendSMS() {
        return sendSMS;
    }

    public void setSendSMS(SendSMS sendSMS) {
        this.sendSMS = sendSMS;
    }
}
